import requests
import json

api_key = 'VG4gt3v6QPUsL4AiKC7dBgml'
secret_key = 'JQcn509xrstxHZ1kY3U7HUcIl8OsiUse'

# 鉴权生成toekn
def get_token(api_key, secret_key):
    url = 'https://aip.baidubce.com/oauth/2.0/token'
    params = {
        'grant_type': 'client_credentials',
        'client_id': api_key,
        'client_secret': secret_key
    }
    response = requests.get(url, params=params)
    result = response.json()
    token = result['access_token']
    return token

# 发送聊天请求
def send_chat_req(message):
    token = get_token(api_key, secret_key)
    url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions'
    headers = {
        'Content-Type': 'application/json'
    }
    messages = [
        {"role": "user", "content": message}
    ]
    data = {
        "messages": messages
    }
    response = requests.post(url, headers=headers, params={'access_token': token}, data=json.dumps(data))
    return response
